package com.sptpc.androidnetworktest;


import androidx.appcompat.app.AppCompatActivity;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

import static android.util.Log.e;

public class MainActivity extends AppCompatActivity {
    EditText userAccount;
    EditText password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button connetNET = findViewById(R.id.connectNet);
        userAccount = findViewById(R.id.editTextUser);
        password = findViewById(R.id.editTextPassword);
        connetNET.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String userName = userAccount.getText().toString();
                final String userPwd = password.getText().toString();
                //线程
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        //执行以下代码
                        try {
                            // 创建OkHttpClient对象
                            OkHttpClient client = new OkHttpClient();
                            // 创建一个请求(Get)
                            Request request = new Request.Builder().url("http://192.168.43.223/html/login.jsp?username="+userName+"&psd="+userPwd).build();
                            // 返回实体 发起请求(同步Get请求)
                            Response response = client.newCall(request).execute();
                            /**获取返回的数据，通过response.body().string()获取，默认返回的是utf-8格式；
                             * header主要来存放cookie，token等信息的
                             * body主要用来存放post的一些数据，比如
                             * string()适用于获取小数据信息，如果返回的数据超过1M，建议使用stream()获取返回的数据，
                             * 因为string() 方法会将整个文档加载到内存中。*/
                            final String result = response.body().string();//
                            Log.d("ResultData", result);

                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    // 判断是否成功
                                    if (result.contains("1"))
                                    {
                                        Toast.makeText(getApplicationContext(),"登陆成功",Toast.LENGTH_SHORT).show();
                                        Uri uri = Uri.parse("http://192.168.43.223/index/index.html");
                                        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                                        startActivity(intent);
                                    }else{
                                        Toast.makeText(getApplicationContext(),"登陆失败，检查账号或密码",Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                        }catch (Exception e)
                        {
                            e.printStackTrace();
                        }
                    }
                }).start();


                URL url = null;
//                try {
//                    url = new URL(httpURL);
//                    HttpURLConnection  conn = (HttpURLConnection)url.openConnection();
//                    conn.setDoInput(true);
//                    conn.setConnectTimeout(3000);
//                    conn.setReadTimeout(3000);
//                    conn.setUseCaches(false);
//                    conn.setRequestMethod("GET");
//                    conn.setDoOutput(true);
//                    conn.connect();
//                    String responseData = conn.getResponseMessage();
//                    InputStream is = conn.getInputStream();
//                    InputStreamReader isr = new InputStreamReader(is,"utf-8");
//                    BufferedReader br = new BufferedReader(isr);
//                    String resultData;
//                    Toast.makeText(MainActivity.this,responseData,Toast.LENGTH_SHORT);
//                    // 关闭连接
//                    conn.disconnect();
//                    br.close();
//                    isr.close();
//                    is.close();
//                }
//                catch (MalformedURLException e) {
//                    e.printStackTrace();
//                }
//                catch (IOException e) {
//                    e.printStackTrace();
//                }
                //调用conn.setDoOutput()方法以显式开启请求体
//                conn.setDoOutput(true);
//                //设置读取超时的时间
//                conn.setReadTimeout(5000);
//                //设置链接超时的时间
//                conn.setConnectTimeout(5000);
//                //不适用缓存
//                conn.setUseCaches(false);
//                //编码
//                conn.setRequestProperty("Charset","utf-8");
//                //获取请求头
//                requestHead = getReqeustHeader(conn);
            }
        });
    }
}
